<?php $this->_extends('../_layouts/default_layout'); ?>
<?php $this->_block('contents'); ?>
<link type="text/css" href="<?=Q::ini('custom_system/base_url')?>css/jq.autocomplete.css" rel="stylesheet" />
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.autocomplete.js"></script>
<script type="text/javascript">
Refresh = function(){
	//jq的自动完成
	$(".ajax_product").autocomplete("<?=url('Purchase::Order/ProductInfoAjax')?>").result(function(event, data, formatted){
		if (data[0])
		{
			var container = $(this).parent().parent().parent();
			container.find("input.product_number").val(data['number']);
			container.find("input.available_quantity").val(data['available_quantity']);
			container.find("input[name$='product_basic_id]']").val(data['id']);
			container.find("input[name$='actual_price]']").val(data['purchase_price']);
			if (data['preferred_provider_id'])
			{
				container.find("input.actual_provider_id").val(data['preferred_provider_id']);
				container.find("input.ajax_provider").val(data['preferred_provider_id']+data['preferred_provider_name']);
			}
		}
		$("input.ajax_product").focus(function (){if ($(this).val().length*1.6 > 32){$(this).attr("size",$(this).val().length*1.8);}});
		$("input.ajax_product").keyup(function (){if ($(this).val().length*1.6 > 32){$(this).attr("size",$(this).val().length*1.8);}});
	});
	$(".ajax_provider").autocomplete("<?=url('Purchase::Order/Provider')?>").result(function(event, data, formatted){
		if (data[0])
		{
			var container = $(this).parent().parent().parent();
			container.find("input.actual_provider_id").val(data['id']);
		}
	});
};
$(function(){
	//日期
	$(".datepicker").datepicker({dateFormat:'yy-mm-dd',changeMonth:true});
	//删除按钮
	$(".delete").live("click", function(){
		$(this).parent().parent().parent().parent().remove();
	});
	var i = 0;
	$("#add_item").click(function(){
		var _html = "<table width='100%' class='list_table'>";
		_html += "<tr><td align='left' colspan='5'><span>产品编号</span>";
		_html += " <input type='text' size='6' disabled='disabled' class='product_number' />";
		_html += " <input type='text' size='85' class='ajax_product' />";
		_html += "<input type='hidden' name='item[new_" + i + "][product_basic_id]' />";
		_html += " 可用库存 <input type='text' size='3' class='available_quantity' style='border:none' /></td></tr>"
		_html += "<tr><td width='130'>数量 <input type='text' size='5' name='item[new_" + i + "][actual_quantity]' /></td>";
		_html += "<td width='130'>价格 <input type='text' size='5' name='item[new_" + i + "][actual_price]' /></td>";
		_html += "<td width='300'>供应商 <input type='text' size='4' disabled='disabled' class='actual_provider_id' /> <input type='text' class='ajax_provider' /> <input type='hidden' class='actual_provider_id' name='item[new_" + i + "][actual_provider_id]' /></td>";
		_html += "<td>备注 <input type='text' size='50' name='item[new_" + i + "][notes]' /></td>";
		_html += "<td><a class='delete'>删除</a></td></tr></table>";
		$("#item_list").append(_html);
		Refresh();
		i++;
	});
	//保存采购单
	$("#add_save").click(function(){
		var can_save = 1;
		//检查item产品
		$("#item_list").find("input[name$='product_basic_id]']").each(function(){
			if (!$(this).val())
			{
				can_save=0;
				alert("请检查每个item产品编号");
				return false;
			}
		});
		//检查Item数量
		$("#item_list").find("input[name$='actual_quantity]']").each(function(){
			if (!$(this).val())
			{
				can_save=0;
				alert("请检查每个item数量");
				return false;
			}
		});
		//检查item价格
		$("#item_list").find("input[name$='actual_price]']").each(function(){
			if (!$(this).val())
			{
				can_save=0;
				alert("请检查每个item价格");
				return false;
			}
		});
		//检查item供应商
		$("#item_list").find("input[name$='actual_provider_id]']").each(function(){
			if (!$(this).val())
			{
				can_save=0;
				alert("请检查每个item供应商");
				return false;
			}
		});
		//检测是否有重复SKU
		var icp = $("input.product_number"),repeat = false;
		icp.each(function(){
			if (icp.filter('[value="' + this.value + '"]').size()>1)
			{
				repeat = this.value;
			}
		});
		if (repeat !== false) 
		{
			if(!confirm('有重复的产品：' + repeat + '！是否继续操作？'))
			{
				return false;
			}
		}
		//检查周期结款下供应商是否一致
		var icps = $(".actual_provider_id"),repeats = false;
		if($("#purchase_order_payment_type").val()==1)
		{
			icps.each(function(){
				if (icps.filter('[value !="' + this.value + '"]').size()>=1)
				{
					repeats = this.value;
				}
			});
			if (repeats !== false) 
			{
				alert('周期结款下供应商需要一致');
				return false;
			}
		}
		if (can_save)
		{
			$("form").submit();
		}
	});
	Refresh();
});
</script>
<form method="post" action="<?=url('Purchase::Order/DrawbackEditSave')?>">
	<table class="form_table">
		<tr>
			<th>订单号</th>
			<td><?=$purchase_order['number']?><input type="hidden" name="id" value="<?=$purchase_order['id']?>" /></td>
			<th>仓库</th>
			<td><?=$purchase_order['warehouse_name']?></td>
		</tr>
		<tr>
			<th>日期</th>
			<td><input type="text" name="purchase_time" value="<?=$purchase_order['purchase_time']?>" class="datepicker" /></td>
			<th>经手人员</th>
			<td>
				<select name="purchase_user_id">
					<? foreach ($user as $u):?>
					<option value="<?=$u['id']?>" <?if ($u['id'] == CURRENT_USER_ID):?> selected="selected" <?endif;?>><?=$u['name']?></option>
					<? endforeach;?>
				</select>
			</td>
		</tr>
		<tr>
			<th>结款类型</th>
			<td>
				<select name="purchase_order_payment_type" id="purchase_order_payment_type">
					<? foreach (Q::ini('custom_flag/purchase_order_payment_type') as $pt):?>
					<option value="<?=$pt['value']?>" <?if ($purchase_order['payment_type'] == $pt['value'])echo "selected='selected'" ?>><?=$pt['name']?></option>
					<? endforeach;?>
				</select>
			</td>
		</tr>
		<tr>
			<th>备注</th>
			<td colspan="3"><textarea rows="3" cols="60" name="notes"><?=$purchase_order['notes']?></textarea></td>
		</tr>
	</table>
<div id="item_list">
<?foreach ($item as $i):?>
<table width="100%" class="list_table">
	<tr>
		<td align="left" colspan="5">
			<span>产品编号</span>
			<input type="text" size="6" disabled="disabled" class="product_number" value="<?=$i['product_number']?>" />
			<input type="text" size="<?=strlen($i['product_name']) ?>" class="ajax_product" value="<?=$i['product_number']?> <?=$i['product_name']?>" />
			<input type="hidden" name="item[<?=$i['id']?>][product_basic_id]" value="<?=$i['product_basic_id']?>" />
			可用库存 <input type="text" size="3" class="available_quantity" style="border:none" value="<?=$i['available_quantity']?>"/> 
		</td>
	</tr>
	<tr>
		<td width="120">数量 <input type="text" size="5" name="item[<?=$i['id']?>][actual_quantity]" value="<?=$i['actual_quantity']?>" /></td>
		<td width="120">价格 <input type="text" size="5" name="item[<?=$i['id']?>][actual_price]" value="<?=$i['actual_price']?>" /></td>
		<td width="300">供应商 
			<input type="text" size="4" disabled="disabled" class="actual_provider_id" value="<?=$i['actual_provider_id'] ?>" />
			<input type="text" size="30" class="ajax_provider" value="<?=$i['actual_provider_name']?>" />
			<input type="hidden" class="actual_provider_id" name="item[<?=$i['id']?>][actual_provider_id]" value="<?=$i['actual_provider_id'] ?>" />
		</td>
		<td>备注 <input type="text" size="50" name="item[<?=$i['id']?>][notes]" value="<?=$i['notes'] ?>" /></td>
		<td><a class="delete">删除</a></td>
	</tr>
</table>
<?endforeach;?>
</div>
<input type="hidden" name="history" value="<?=$_SERVER['HTTP_REFERER']?>" />
<a id="add_item">添加产品</a>
<div align="center"><input type="button" class="button" id="add_save" value="保存退货单" /><input type="hidden" name="<?=Q::ini('unique_token_name');?>" value="<?=Helper_BSS_Normal::genToken();?>"/></div>
</form>
<?php $this->_endblock();?>